package com.shy.service.impl;

import com.shy.entity.DicValue;
import com.shy.mapper.DicValueMapper;
import com.shy.service.DicValueService;
import jakarta.annotation.Resource;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@CacheConfig(cacheNames = "com.shy.service.impl.DicValueServiceImpl")
public class DicValueServiceImpl implements DicValueService {
    @Resource
    private DicValueMapper dicValueMapper;
    @Resource
    private CacheManager cacheManager;

    @Override
    @Cacheable(key = "#typeCode")
    public List<DicValue> queryDicValueByCode(String typeCode) {
        return dicValueMapper.queryDicValueByCode(typeCode);
    }
    @Override
    @CacheEvict(allEntries = true)
    public void evictAllCaches() {
        // 清除所有缓存
        cacheManager.getCacheNames().forEach(cacheName -> Objects.requireNonNull(cacheManager.getCache(cacheName)).clear());
    }

}
